/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : CAN Bus Communication Example
 ******************************************************************************
 */

#include "stm32f4xx.h"
#include "./BSP/CAN/bsp_can.h"
#include "./BSP/USART/bsp_usart.h"
#include <stdio.h>

void can_rx_callback(CAN_Message_t *msg)
{
    printf("CAN RX: ID=0x%03X, DLC=%d, Data=", msg->id, msg->dlc);
    for (int i = 0; i < msg->dlc; i++)
    {
        printf("%02X ", msg->data[i]);
    }
    printf("\r\n");
}

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    usart_init(115200);

    printf("CAN Bus Communication Example\r\n");

    can_init();
    can_set_rx_callback(can_rx_callback);

    printf("CAN initialized\r\n");

    uint32_t count = 0;

    while (1)
    {
        uint8_t data[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
        can_send_std(0x123, data, 8);

        printf("CAN TX: ID=0x123, Data=01 02 03 04 05 06 07 08\r\n");

        HAL_Delay(1000);
        count++;

        if (count >= 10)
        {
            break;
        }
    }

    printf("CAN example completed\r\n");

    while (1)
    {
        HAL_Delay(1000);
    }
}

void SystemClock_Config(void)
{
    /* 同上 */
}
